﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Weight.Controls
{
    public class LYFlButton : Control
    {
        public LYFlButton()
        {
            this.SetStyle(
                 //ControlStyles.UserPaint |//使用自定义的绘制方式
                 ControlStyles.ResizeRedraw//当控件大小发生变化时就重新绘制
                | ControlStyles.SupportsTransparentBackColor//则控件接受 alpha 组件数小于 255 个的 BackColor 来模拟透明度
                | ControlStyles.AllPaintingInWmPaint//则控件忽略窗口消息 WM_ERASEBKGND 以减少闪烁
                | ControlStyles.OptimizedDoubleBuffer //则控件将首先绘制到缓冲区而不是直接绘制到屏幕，这可以减少闪烁
                | ControlStyles.DoubleBuffer
                , true);
            this.Size = new Size(140, 38);
            borderRect = new Rectangle(0, 0, this.Width - 1, this.Height - 1);
            this.BackColor = Color.Transparent;
        }
        #region 属性

        private CoolButtonStyle _coolStyle;
        [System.ComponentModel.Browsable(true), DefaultValue(CoolButtonStyle.Normal)]
        public CoolButtonStyle CoolStyle
        {
            get => _coolStyle;
            set
            {
                _coolStyle = value;
                this.Invalidate();
            }
        }

        private Color _borderColor = Color.FromArgb(0, 163, 129);
        [System.ComponentModel.Browsable(true)]
        [System.ComponentModel.DefaultValue(typeof(Color), "0, 163, 129")]
        public Color BorderColor
        {
            get => _borderColor;
            set
            {
                _borderColor = value;
                this.Invalidate();
            }
        }

        private int _borderWidth = 1;
        [System.ComponentModel.Browsable(true)]
        [System.ComponentModel.DefaultValue(typeof(int), "1")]
        public int BorderWidth
        {
            get => _borderWidth;
            set
            {
                _borderWidth = value;
                this.Invalidate();
            }
        }

        private Image _image;
        [System.ComponentModel.Browsable(true)]
        [System.ComponentModel.DefaultValue(typeof(Image), "Properties.Resources.ButtonImage")]
        public Image Image
        {
            get => _image;
            set
            {
                _image = value;
                this.Invalidate();
            }
        }

        private string _text = nameof(LYFlButton);
        [System.ComponentModel.Browsable(true)]
        [System.ComponentModel.DefaultValue(typeof(string), nameof(LYFlButton))]
        public new string Text
        {
            get => _text;
            set
            {
                _text = value;
                this.Invalidate();
            }
        }

        private Rectangle _imageRect = new Rectangle(10, 10, 20, 20);
        [System.ComponentModel.Browsable(true)]
        [System.ComponentModel.DefaultValue(typeof(Point), "10, 10, 20, 20")]
        public Rectangle ImageRect
        {
            get => _imageRect;
            set
            {
                _imageRect = value;
                this.Invalidate();
            }
        }

        private ContentAlignment _textAlign = ContentAlignment.MiddleCenter;
        [System.ComponentModel.Browsable(true)]
        [System.ComponentModel.DefaultValue(typeof(ContentAlignment), "MiddleCenter")]
        public ContentAlignment TextAlign
        {
            get => _textAlign;
            set
            {
                _textAlign = value;
                this.Invalidate();
            }
        }


        private bool _isInformation = true;
        [System.ComponentModel.Browsable(true)]
        [System.ComponentModel.DefaultValue(typeof(bool), "true")]
        public bool IsInformation
        {
            get => _isInformation;
            set
            {
                _isInformation = value;
                this.Invalidate();
            }
        }


        private int _informationValue = 1;
        [System.ComponentModel.Browsable(true)]
        [System.ComponentModel.DefaultValue(typeof(int), "1")]
        public int InformationValue
        {
            get => _informationValue;
            set
            {
                _informationValue = value;
                this.Invalidate();
            }
        }


        private string _toolTipText = "coolButton1";
        [System.ComponentModel.Browsable(true)]
        [System.ComponentModel.DefaultValue(typeof(string), "coolButton1")]
        public string ToolTipText { get => _toolTipText; set => _toolTipText = value; }

        private double _opacity = 75;
        [System.ComponentModel.Browsable(true)]
        [System.ComponentModel.DefaultValue(typeof(double), "75")]
        public double Opacity
        {
            get => _opacity;
            set
            {
                if (value >= 0 && value <= 100)
                    _opacity = value;
                else if (value < 0)
                    _opacity = 0;
                else if (value > 100)
                    _opacity = 100;
                this.Invalidate();
            }
        }

        private Color _transparentColor = Color.FromArgb(0, 163, 129);
        [System.ComponentModel.Browsable(true)]
        [System.ComponentModel.DefaultValue(typeof(Color), "0, 163, 129")]
        public Color TransparentColor
        {
            get => _transparentColor;
            set
            {
                _transparentColor = value;
                this.Invalidate();
            }
        }

        #endregion

        #region 自定义
        private Rectangle borderRect;
        private Point textPoint;

        private MouseStatus mouseStatusBorder = default(MouseStatus);
        private MouseStatus mouseStatusBack = default(MouseStatus);
        #endregion

        //protected override CreateParams CreateParams
        //{
        //    get
        //    {
        //        CreateParams cp = base.CreateParams;
        //        cp.ExStyle = 0x20;
        //        return cp;
        //    }
        //}


        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            //if (this.Width > 0 && this.Height > 0)
            //{
            //    Image backImg = new Bitmap(this.Width, this.Height);
            //    using (Graphics g = Graphics.FromImage(backImg))
            //    {
            //        if (mouseStatusBack == MouseStatus.Enter)
            //            g.FillRectangle(new SolidBrush(Color.FromArgb(((int)(_opacity * 255 / 100) + 30) > 255 ? 255 : ((int)(_opacity * 255 / 100) + 30), _transparentColor)), new Rectangle(0, 0, this.Width, this.Height));

            //        else
            //            g.FillRectangle(new SolidBrush(Color.FromArgb((int)(_opacity * 255 / 100), _transparentColor)), new Rectangle(0, 0, this.Width, this.Height));
            //    }
            //    e.Graphics.DrawImage(backImg, new Rectangle(0, 0, this.Width, this.Height));
            //}
            Color color = Color.FromArgb(0, _transparentColor);
            e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb((int)(_opacity * 255 / 100), color)), e.ClipRectangle);
            //绘制边框效果
            if (_borderWidth > 0)
            {
                if (mouseStatusBorder == MouseStatus.Enter)
                    e.Graphics.DrawRectangle(new Pen(_borderColor, _borderWidth), borderRect);
                else
                    e.Graphics.DrawRectangle(new Pen(Color.FromArgb(100, _borderColor), _borderWidth), borderRect);
            }
            //按钮图像
            if (_image != null)
                e.Graphics.DrawImage(_image, _imageRect);

            #region 文字位置设置
            int textWidth = (int)e.Graphics.MeasureString(_text, this.Font).Width;
            int yBottom = this.Height - this.Font.Height - 8;
            int yCenter = (this.Height - this.Font.Height) / 2;
            int yTop = 8;

            int xLeft = 8;
            int xCenter = (this.Width - textWidth) / 2;
            int xRight = this.Width - textWidth - 8;
            if (_textAlign == ContentAlignment.BottomCenter)
                textPoint = new Point(xCenter, yBottom);
            else if (_textAlign == ContentAlignment.BottomLeft)
                textPoint = new Point(xLeft, yBottom);
            else if (_textAlign == ContentAlignment.BottomRight)
                textPoint = new Point(xRight, yBottom);

            else if (_textAlign == ContentAlignment.MiddleCenter)
                textPoint = new Point(xCenter, yCenter);
            else if (_textAlign == ContentAlignment.MiddleLeft)
                textPoint = new Point(xLeft, yCenter);
            else if (_textAlign == ContentAlignment.MiddleRight)
                textPoint = new Point(xRight, yCenter);

            else if (_textAlign == ContentAlignment.TopCenter)
                textPoint = new Point(xCenter, yTop);
            else if (_textAlign == ContentAlignment.TopLeft)
                textPoint = new Point(xLeft, yTop);
            else if (_textAlign == ContentAlignment.TopRight)
                textPoint = new Point(xRight, yTop);
            e.Graphics.DrawString(_text, this.Font, new SolidBrush(this.ForeColor), textPoint);
            #endregion

            if (IsInformation)
            {
                int x = _imageRect.Right - 6 < 0 ? 0 : _imageRect.Right - 6;
                int y = _imageRect.Y - 6 < 0 ? 0 : _imageRect.Y - 6;
                int width = 12;
                int height = width;
                e.Graphics.FillEllipse(new SolidBrush(Color.Red), x, y, width, height);
                int valueWidth = (int)e.Graphics.MeasureString(_informationValue + "", this.Font).Width;
                e.Graphics.DrawString(_informationValue.ToString(), this.Font, new SolidBrush(Color.White), new Point(x + (width - valueWidth) / 2 + 1, y + (height - this.Font.Height) / 2));
            }
            base.OnPaint(e);
        }

        protected override void OnClick(EventArgs e)
        {
            //this.BackColor = Color.Red;
            base.OnClick(e);
        }

        protected override void OnMouseEnter(EventArgs e)
        {
            mouseStatusBack = MouseStatus.Enter;
            mouseStatusBorder = MouseStatus.Enter;
            this.Invalidate();

            ToolTip tip = new ToolTip();
            // Set up the delays for the ToolTip.
            tip.AutoPopDelay = 5000;
            tip.InitialDelay = 800;
            tip.ReshowDelay = 200;
            // Force the ToolTip text to be displayed whether or not the form is active.
            tip.ShowAlways = true;
            // Set up the ToolTip text for the Button and Checkbox.
            tip.SetToolTip(this, this._toolTipText);
            base.OnMouseEnter(e);
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            mouseStatusBack = MouseStatus.Leave;
            mouseStatusBorder = MouseStatus.Leave;
            this.Invalidate();
            base.OnMouseLeave(e);
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            borderRect = new Rectangle(0, 0, this.Width - 1, this.Height - 1);
            base.OnSizeChanged(e);
        }

        public void SetSkin(Color color)
        {
            this.BorderColor = color;
            this.TransparentColor = color;
        }

        public enum MouseStatus
        {
            None,
            Enter,
            Leave,
        }
    }
    public enum CoolButtonStyle
    {
        Normal,
        Small,
        Large,
    }


}
